string^ 与CString 转换

来源:百度知道 编辑:UC知道 时间:2024/06/30 09:35:58
在C++中引用到了一个函数,其中参数是string^ 类型的,但是得到到参数全是CString类型的,怎么转换下?
我自己把这个问题 解决了。如下:
CString cstring;
String^ str=gcnew String(cstring);

string^??
如果是string类型的话
可以这样转换
CString cs;
string str;
str=string(cs.GetBuffer(0));

反之
cs=CString(str.c_str());

CString str;
str.Format("%s",string.c_str);

比如有函数void fun(string str){}
可以这样调用fun:

CString cstring="Fun";
fun(cstring.GetBuffer(100));
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

CString::GetBuffer

LPTSTR GetBuffer( int nMinBufLength );
throw( CMemoryException );

返回值:一个指向对象的(以空字符结尾的)字符缓冲区的LPTSTR指针。
LPTSTR也就是一个字符串指针,可以用LPTSTR来作为参数传给fun,因为string可以由char *来初始化